﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWPF.DataBinding
{
    /// <summary>
    /// Data Error方式的数据校验
    /// </summary>
    public class NativeVMDataBindingViewModel : IDataErrorInfo,INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 学生姓名
        /// </summary>
        private string _student_Name;
        public string Student_Name
        {
            get { return _student_Name; }
            set
            {
                _student_Name = value;
                OnPropertyChanged("Student_Name");
            }
        }

        private string _error = string.Empty;
        public string Error => _error;

        public string this[string propertyName]
        {
            get
            {
                if(propertyName == nameof(Student_Name))
                {
                    if(Student_Name == "张三123")
                    {
                        _error = "";
                    }
                    else
                    {
                        _error = "名字必须输入张三123";
                    }
                }

                return _error;
            }
        }

        /// <summary>
        /// Ctor method
        /// </summary>
        public NativeVMDataBindingViewModel()
        {
            Student_Name = "张三";
            //Task.Run(async () =>
            //{
            //    await Task.Delay(6000);
            //    Student_Name = "李四";
            //});
        }
    }
}
